#include "backgroundrollingscene.h"
#include"landsprite.h"
#include"globalgamemsger.h"
#include"birdsprite.h"
#include"pipesprite.h"
#include"scoresprite.h"
#include"spacecontrolledpipesprite.h"
#include"notesprite.h"
#include<QPainter>
#include<QTimer>
void BackGroundRollingScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    for(int i=offset;i<=rect.right();i+=backGroundimg.width())
    {
        painter->drawImage(i,0,backGroundimg);
    }
}

void BackGroundRollingScene::drawForeground(QPainter *painter, const QRectF &rect)
{
    QRectF ImgRect=GameOverImg.rect();
    QImage KeyS(":/ImgSrc/Images/UI/KeyS.png"),KeyD(":/ImgSrc/Images/UI/KeyD.png");
    QImage KeySpace(":/ImgSrc/Images/UI/SPACE.png");
    QImage soundHint(":/ImgSrc/Images/UI/SoundTransparent.png");
    KeySpace=KeySpace.scaledToWidth(80);
    switch(state)
    {
    case gameover:
        painter->drawImage(rect.center().x()-ImgRect.width()/2,rect.center().y()-ImgRect.height()/2,GameOverImg);
        break;
    case playing:
        break;
    case waiting:
        painter->drawImage(rect.width()/2-15-KeyS.rect().width(),rect.center().y()+60,KeyS);
        painter->drawImage(rect.width()/2-15+KeyD.rect().width()/2,rect.center().y()+60,KeyD);
        painter->drawImage(rect.width()/2-KeySpace.rect().width()/2,rect.center().y()+140,KeySpace);
        painter->drawImage(rect.width()/2-soundHint.rect().width()/2,rect.center().y()+180,soundHint);
        break;
    }
}

BackGroundRollingScene::BackGroundRollingScene():offset(0.0),runSpd(1),randomGenerator(QRandomGenerator::global())
{
    connect(&globalTicker,&QTimer::timeout,this,&BackGroundRollingScene::tick);
    connect(&globalGameMsger::m,&globalGameMsger::gameBegin,this,&BackGroundRollingScene::onGameBegin);
    connect(&globalGameMsger::m,&globalGameMsger::gameOver,this,&BackGroundRollingScene::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameRestart,this,&BackGroundRollingScene::onGameRestart);
    GameOverImg.load(":/ImgSrc/Images/GAMEOVER.png");
    //PipeCountDown=randomGenerator->bounded(pipeSummonT,pipeSummonTmax);
}

BackGroundRollingScene::tagState BackGroundRollingScene::getstate() const
{
    return state;
}
void BackGroundRollingScene::tick()
{
    offset-=runSpd;
    if(state==playing)PipeCountDown--;
    if(state==playing && PipeCountDown<=0)
    {
        float H=sceneRect().height();
        float h=randomGenerator->bounded(906.0+gap-H+70);
        //new PipeSprite(*this,1,-h,3);
        //new PipeSprite(*this,0,-h+gap+453.0,3);

        float k=randomGenerator->bounded(0.6)+0.8;
        //addItem(new SpaceControlledPipeSprite(sceneRect().width(),-h          ,0.2, 0      ,-906-gap+H-70,0,k));
        //addItem(new SpaceControlledPipeSprite(sceneRect().width(),-h+gap+453.0,0.2, 453+gap,H-453-70     ,1,k));
                                                                                                //150:大GAP
        addItem(new SpaceControlledPipeSprite(sceneRect().width(),-h          ,0.2, 0      ,-906-150+H-70,0,k));
        addItem(new SpaceControlledPipeSprite(sceneRect().width(),-h+gap+453.0,0.2, 453+gap,H-453-70     ,1,k));
        PipeCountDown=randomGenerator->bounded(pipeSummonT,pipeSummonTmax);
    }
}

void BackGroundRollingScene::onGameBegin()
{
    state=playing;
    runSpd=speed;
    PipeCountDown=randomGenerator->bounded(pipeSummonT,pipeSummonTmax);
}

void BackGroundRollingScene::onGameOver()
{
    state=gameover;
    runSpd=0;
}

void BackGroundRollingScene::onGameRestart()
{
    state=waiting;
    runSpd=1;
}

void BackGroundRollingScene::Init()
{
    state=waiting;
    backGroundimg.load(":/ImgSrc/Images/background.png");
    backGroundimg=backGroundimg.copy(1,0,backGroundimg.width()-2,backGroundimg.height()-2);
    QRectF r=sceneRect();
    backGroundimg=backGroundimg.scaledToHeight(r.height());
    addItem(new BirdSprite(r.width()/2,r.height()/2,0.1));
    for(int i=0;i<=sceneRect().width();i+=landSprite::landImage.rect().width())
    {
        landSprite*landSpr=new landSprite;
        addItem(landSpr);
        landSpr->setPos(i,sceneRect().height()-landSprite::landImage.rect().height());
    }
    addItem(s=new ScoreSprite(this));
    note=new NoteSprite;
    addItem(note);
    note->setPos(0,0);
    note->setDefaultTextColor(Qt::white);
    QFont f=note->font();
    f.setPointSize(108);
    note->setFont(f);
    note->setZValue(5);
}
int BackGroundRollingScene::getScore() const
{
    return s->Score;
}

void BackGroundRollingScene::setNote(const QString&NoteText,const Qt::GlobalColor col)
{
    note->setPlainText(NoteText);
    note->setDefaultTextColor(col);
}

void BackGroundRollingScene::setNoteCol(const Qt::GlobalColor col)
{
    note->setDefaultTextColor(col);
}
void BackGroundRollingScene::setNoteCol(const QRgb col)
{
    note->setDefaultTextColor(col);
}

void BackGroundRollingScene::setNotePoint(float pnt)
{
    QFont f=note->font();
    f.setPointSizeF(pnt);
    note->setFont(f);
}
